package cn.sciento.farm.devicemanager.gateway.model.downlink.fertilizer;

import cn.sciento.farm.devicemanager.common.constant.MqttConstant;
import cn.sciento.farm.devicemanager.gateway.model.downlink.AbstractBlockData;
import cn.sciento.farm.devicemanager.gateway.model.downlink.MqttContent;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.HashMap;
import java.util.Map;

@Data
@EqualsAndHashCode(callSuper = false)
public class FertilizerControlData extends AbstractBlockData {

    private String eui;

    private Integer modbusAddr;

    private Integer fertilizeValve;


    private Integer stirringMotor;

    private Float fertilizeRatio;

    private Integer channelNo;

    private Integer fertilizeTime;

    private Integer mainControl;



    @Override
    public String generateId() {
        return String.format(MqttConstant.FERTILIZER_CONTROL_SUB_BLOCK_ID, this.eui);
    }

    @Override
    public String generateStatus() {
        return null;
    }

    @Override
    public MqttContent generate() {
        String topic = String.format(MqttConstant.FERTILIZER_DOWN_TOPIC, this.eui);
        Map map = new HashMap<String,Object>();
        map.put("id",modbusAddr);
        map.put("channel",channelNo);
        map.put("stirringMotor",stirringMotor);
        map.put("fertilizeValve",fertilizeValve);
        if(fertilizeRatio != null){
            map.put("fertilizeRatio",new Integer((int)(fertilizeRatio*10)));
        }
        if(fertilizeTime != null){
            map.put("fertilizeTime",fertilizeTime);
        }
        map.put("mainControl",mainControl);
        String payload = JSON.toJSONString(map);
        return new MqttContent(topic,payload);
    }
}
